const url = "https://lib-seat.chu.edu.cn/ClientWeb/xcus/ic2/Default.aspx"

const puppeteer = require("puppeteer")

const sleep = time => new Promise(resolve => {
    setTimeout(resolve, time)
})


const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
    service: 'qq',
    port: 465,
    secureConnection: true,
    auth: {
        user: '846642539@qq.com',
        pass: 'bhtzsgtygbjpbajc',
    }
});

const mailOptions = {
    from: '"zhangyue" <846642539@qq.com>',
    to: '1445005457@qq.com',
    subject: '早上好啊亲爱的',
    html: '<b>座位已经抢到啦,今天学习加油</b>'
};

// // send mail with defined transport object
// transporter.sendMail(mailOptions, (error, info) => {
//   if (error) {
//     return console.log(error);
//   }
//   console.log('Message sent: %s', info.messageId);
//   // Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com>
// });

var CronJob = require('cron').CronJob;
new CronJob('10,30,50 30,31,32,33,34,35,36 5 * * *', async () => {
    console.log("开始占座")
    console.log(Date.now())
    const broswer = await puppeteer.launch({
        headless: false,
        args: ['--no-sandbox'],
        dumpio: false
    })

    //浏览器不显示
    // await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});  

    const page = await broswer.newPage()

    await page.goto(url, {
        waitUntil: 'networkidle2'
    })
    await sleep(1000)

    // var zuoweis = $(".fp-dot fp-dot-ok")

    // var jinru = $('.nav_cls_li cls_sec activity')
    await page.click(".nav_cls_name")
    await page.click(".it")
    console.log(1)
    // await page.waitForSelector(".fp-dot fp-dot-ok")
    await sleep(3000)
    console.log(2)
    // await page.click('.fp-dot fp-dot-ok')
    // console.log(jinru)
    const result = await page.evaluate(async () => {
        // var click1 =  document.getElementsByClassName("nav_cls_name")
        // click1[0].click()
        // var click2 = document.getElementsByClassName("it")
        // click2[0].click()
        var list = document.getElementsByClassName("fp-dot fp-dot-ok")
        for (var i = 0; i < list.length; i++) {
            if (list[i].click()) {
                console.log("已点击")
                break;
            }
            else {
                console.log("继续点击")
                continue;
            }
        }
        list[1].click()
        var id = document.getElementsByName("id")
        id[2].value = "18033078"
        var pwd = document.getElementsByName("pwd")
        pwd[2].value = "280329"

        // setTimeout(() => {
        var btn = await document.getElementsByClassName("btn")

        await btn[12].click()
        // }, 3000);

        console.log("正在确认")
        console.log("尝试获取确认按钮")
        setTimeout(async () => {
            var tijiao = document.getElementsByClassName("btn btn-info")
            console.log("得到确认按钮")
            console.log(tijiao.length)
            for (let i = 0; i < tijiao.length; i++) {
                await tijiao[i].click()
                console.log("点击" + i)
                await tijiao[i].click()
                await tijiao[i].click()
                console.log("点击" + i)
                await tijiao[i].click()
            }
            console.log("抢座 执行完毕")
            // 发邮件
            transporter.sendMail(mailOptions, (error, info) => {
                if (error) {
                    return console.log(error);
                }
                console.log('Message sent: %s', info.messageId);
                // Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com>
            });
        }, 2000);
    })
}, null, true, 'Asia/Shanghai')